import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path:"/",
    redirect:"/home"
  },
  {
    path: '/home',
    name: 'home',
    component: ()=>import('../views/HomeView.vue'),
    redirect:"/home/index",
    children:[
      {
        path:"index",
        name:"index",
        component:()=>import('../views/navBarPage/Index.vue'),
        meta:{
          title:"首页 —— 达达外卖"
        }
      },
      {
        path:"search",
        name:"search",
        component:()=>import('../views/navBarPage/Search.vue'),
        meta:{
          title:"搜索"
        }
      },
      {
        path:"order",
        name:"order",
        component:()=>import('../views/navBarPage/Order.vue'),
        meta:{
          title:"订单"
        }
      },
      {
        path:"uCenter",
        name:"uCenter",
        component:()=>import('../views/navBarPage/Ucenter.vue'),
        meta:{
          title:"个人用户中心"
        }
      }
    ]
  },
  {
    path:"/ULogin",
    name:"ULogin",
    component:()=>import('../views/ULogin.vue'),
    meta:{
      title:"用户登录"
    }
  },
  {
    path:"/userDetails",
    name:"userDetails",
    component:()=>import('../views/user/UserDetails.vue'),
    meta:{
      title:"用户个人中心"
    }
  },
  {
    path:"/selectArea",
    name:"selectArea",
    component:()=>import("../views/SelectArea.vue"),
    meta:{
      title:"选择区域"
    }
  },
  {
    path:"/updatePwd",
    name:"updatePwd",
    component:()=>import("../views/user/UpdatePwd.vue"),
  },
  {
    // 天气预报
    path:"/Weather",
    name:"Weather",
    component:()=>import('../views/Weather.vue')
  },
  {
    path:"/address",
    name:"address",
    component:()=>import('../views/Address.vue')
  },
  {
    // 新增地址
    path:"/Addaddress",
    name:"Addaddress",
    component:()=>import("../views/address/Addaddress.vue")
  },
  {
    // 商家详情
    path:"/merchant",
    name:"merchant",
    component:()=>import("../views/merchant/Merchant.vue"),
    redirect:"/merchant/goods",
    children:[
      {
        path:"goods",
        name:"good",
        component:()=>import("../views/merchant/Goods.vue")
      },
      {
        path:"Rating",
        name:"Rating",
        component:()=>import("../views/merchant/Rating.vue")
      },
      {
        path:"info",
        name:"info",
        component:()=>import("../views/merchant/Info.vue")
      }
    ]
  },
  {
    // 充值
    path:"/addMoney",
    name:"addMoney",
    component:()=>import("../views/money/addMoney.vue")
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
    document.title=to.meta.title?to.meta.title:"达达外卖"

    if(to.path=="/updatePwd" || to.path=="/address" || to.path=="/addMoney"){
        if(sessionStorage.getItem("token")){
             next()
        } else {
           return 
        }
    }

    next()
})

export default router
